﻿/**
 * Copyright (c) 2008 michiyasu wada
 * http://www.seyself.com/
 * 
 * Distributed under The MIT License.
 * [http://www.opensource.org/licenses/mit-license.php]
 */

package demo.scenes 
{
import demo.objects.items.SearchBox;
import jp.progression.casts.CastButton;
import demo.buttons.ItemClipButton;
import demo.objects.BodyCast;
import flash.display.DisplayObject;
import flash.events.Event;
import jp.progression.commands.AddChild;
import jp.progression.commands.DoTweener;
import jp.progression.commands.SerialList;
import jp.progression.core.commands.Command;
import jp.progression.events.SceneEvent;
import jp.progression.scenes.SceneId;
import jp.progression.scenes.SceneObject;
import demo.modules.AmazonSearch;
import demo.objects.MainCast;
import demo.objects.HeaderCast;


public class CategoryScene extends SceneObject
{
    
    //--------------------------------------------------------------------------
    //  Properties
    //--------------------------------------------------------------------------
    public var sceneTab:CastButton;
    public var nodeNo:uint;
    
    private var previousSceneId:SceneId;
    
    //--------------------------------------------------------------------------
    //  Constructor
    //--------------------------------------------------------------------------
    public function CategoryScene(name:String, nodeNo:uint, title:String="") 
    {
        super(name);
        
        this.nodeNo = nodeNo;
        this.title = title;
    }
    
    //--------------------------------------------------------------------------
    //  Override Methods
    //--------------------------------------------------------------------------
    override protected function _onInit():void
    {
        var departedId:String = progression.departedSceneId.toString();
        if ( departedId.indexOf("/detail/") < 0 &&
             departedId.indexOf("/about")   < 0 &&
             departedId.indexOf("/terms")   < 0 &&
             departedId.indexOf("/privacy") < 0    )
        {
            Main.cast.body.selection( sceneTab );
            Main.cast.header.search.addEventListener( "enter" , searchEnterHandler );
            AmazonSearch.rankSearch( nodeNo, name , indexSearchCallback );
            this.title = this.title + "｜" + progression.root.title;
        }
    }
    
    override protected function _onGoto():void
    {
        var departedId:String = progression.destinedSceneId.toString();
        if ( departedId.indexOf("/detail/") < 0 &&
             departedId.indexOf("/about")   < 0 &&
             departedId.indexOf("/terms")   < 0 &&
             departedId.indexOf("/privacy") < 0    )
        {
            Main.cast.header.search.removeEventListener( "enter" , searchEnterHandler );
            Main.cast.body.menu.removeItems();
            Main.cast.body.removeItems();
        }
    }
    
    
    //--------------------------------------------------------------------------
    //  Event Handler Methods
    //--------------------------------------------------------------------------
    private function searchEnterHandler( event:Event ):void
    {
        _keywordSearch();
    }
    
    //--------------------------------------------------------------------------
    //  Private Methods
    //--------------------------------------------------------------------------
    private function _keywordSearch():void
    {
        Main.cast.body.removeItems();
        AmazonSearch.keywordSearch( Main.cast.header.search.input.text, name , keywordSearchCallback );
    }
    
    private function indexSearchCallback( data:XML ):void
    {
        Main.cast.body.menu.setData( data.children() );
        
        AmazonSearch.keywordSearch( Main.cast.header.search.input.text , name , keywordSearchCallback );
        
        dispatchEvent( new Event( Event.COMPLETE ));
    }
    
    private function keywordSearchCallback( data:XML ):void
    {
        Main.cast.body.setData(data);
        dispatchEvent( new Event( Event.COMPLETE ));
    }
    
    
    
}

}